﻿import mysql.connector;
import requests;
import json;
# from PIL import Image, ImageDraw, ImageFont
import os,base64 
# import pytesseract
import sys;
class toke():

	def __int__(self,name_i):

		self.name_i=name_i

	def getToke(self,name_i):

		mydb = mysql.connector.connect(
		  host="localhost",       # 数据库主机地址
		  user="root",    # 数据库用户名
		  passwd="root",   # 数据库密码
		  database="weizandata"
		)
		mycursor = mydb.cursor()
		sql="SELECT toke FROM toke WHERE name_i ='"+ name_i +"';"
		mycursor.execute(sql)
		myresult = mycursor.fetchall()     # fetchall() 获取所有记录

		return myresult[0][0]

	def getWxToke(self,appid,secret):

		params={'grant_type':'client_credential','appid':appid,'secret':secret}

		access_token=requests.get(url='https://api.weixin.qq.com/cgi-bin/token',data=params)

		print(access_token)
 
		return access_token;

	def getWxUserInfo(self,uid):

		tk=self

		url='https://api.yunque360.com/v1/worker/user/info?uid='+uid

		#parmeg={'page':str(page),'order_by':'last_event_time','pagesize':str(pagesize)} #'has_user':1,'last_event_time_start':'2020-07-20 00:00:00','last_event_time_end':'2020-07-21 23:59:59',
		
		header=	{'Authorization':tk.getToke('yq')}


		r=requests.get(url=url,headers=header)

		obj=json.loads(r.text)['result']

		userInfo={'nickname':obj['nickname'],'avatar':obj['avatar']}

		return userInfo

	
	def upLieBianToke(self):

		codeUrl='https://w.xiaoliebian.com/api/demo-user-role/oauth/code?';

		rCode=requests.get(url=codeUrl).text;  #登录

		rCodi=json.loads(rCode) # 获取base64和id

		baseCode=rCodi['data']['code'] #base64码

		code=self.suporYingCode('dn',baseCode) #获取验证码

		id=rCodi['data']['id']; #id

		if(code !=''):

			res=self.lbLogin(id,code)

			bol=res['bool']
		
			if(not bol):

				self.hs(not self.lbLogin(id,code)['bool'])

			return res

		else:

			print('返回空验证码')


	def hs(callhun):

		if(callhun):

			hs(callhun)

		else:

			print('停止')


	def lbLogin(self,id,code):


		loginUrl='https://w.xiaoliebian.com/api/demo-user-role/oauth/login' #登录网站

		header={'Content-Type':'application/json'}

		params=json.dumps({'code':code,'id':id,'password':'QMJY@2020','username':'QUMEIJIAJU'}) #账号密码

		r=requests.post(url=loginUrl,data=params,headers=header).text;  #登录

		loginInfo=json.loads(r)

		if(loginInfo['success']):

			access_token=json.loads(r)['data']['access_token']

			mydb = mysql.connector.connect(
			  host="localhost",       # 数据库主机地址
			  user="root",    # 数据库用户名
			  passwd="root",   # 数据库密码
			  database="weizandata"
			)

			mycursor = mydb.cursor()

			sql="UPDATE toke SET toke='"+ access_token +"' WHERE name_i = 'lb';"


			mycursor.execute(sql)

			mydb.commit()

			return {'result':mycursor.rowcount,'bool':'true'}

		else:

			return {'result':'','bool':'false'}



	def suporYingCode(self,pri_id,base64Code): #识别网站验证码

		url='http://apigateway.jianjiaoshuju.com/api/v_1/yzmCustomized.html'

		Header={'appCode':'AE4658AF7130A529EE12CD609237F1D2','appKey':'AKID3a68d9b399a8ff6e905d1e8f70b14a13','appSecret':'e3d4c19baea3e1426e91b7757328e866'}

		params={'pri_id':pri_id,'v_pic':base64Code}

		r=requests.post(url=url,headers=Header,data=params)

		code=json.loads(r.text)['v_code']

		if(code!=''):

			codeR=code

		else:

			codeR=''

		return codeR

if __name__=='__main__':

	#print(toke().getToke('yq'));

	#print(toke().getWxUserInfo('oW2Wk1VNG0-mJUbKvMphScwmdTNU'))

	print(toke().upLieBianToke())